/**
 * Created by tanbingqin on 2019/1/22.
 */
import {messages, getQueryUrlParam} from "share/common";
import React from 'react'
import {connect} from "react-redux";
import { message, Icon, Popover } from 'antd'
import 'styles/components/template/approve-tag.scss'
import baseService from 'share/base.service'

class ApproveTag extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      isShowTag: false
    }
  }

  componentWillMount() {
    let approverOID = getQueryUrlParam('approverOID');
    baseService.setApproveTag(this.props.entityOID, this.props.loginUser.userOID, approverOID).then(response => {
      if (response.data && response.data.userOID && response.data.userOID !== this.props.loginUser.userOID) {
        this.setState({isShowTag: true});
      } else {
        this.setState({isShowTag: false});
      }
    }).catch(e => {
      let error = e.response.data;
      if (error.validationErrors && error.validationErrors.length) {
        message.error(`${error.validationErrors[0].message}`);
      } else {
        message.error(`${error.message}`);
      }
    });
  }

  render() {
    const {isShowTag} = this.state;
    const content = (
      <div style={{width: 210}}>
        <p>{messages('components.key840')/*当前审批节点为任意人审批通过；*/}</p>
        <p style={{marginBottom: 0}}>{messages('components.key841')/*本单据正由其他人审批。*/}</p>
      </div>
    );
    return isShowTag ? (
      <div className="approve-tag">
        <span className="tag-title">{messages('components.key214')/*审批中*/}</span>
        <Popover content={content} placement="bottomRight">
          <Icon type="info-circle" className="tag-icon"/>
        </Popover>
      </div>
    ) : (
      <div></div>
    )
  }
}

ApproveTag.propTypes = {
  entityOID: React.PropTypes.string //单据oid
};

ApproveTag.defaultProps = {
  entityOID: ''
};

ApproveTag.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    loginUser: state.login.loginUser
  }
}

export default connect(mapStateToProps)(ApproveTag);
